package cn.topevery.garden.dal.dao.impl;

import cn.topevery.garden.dal.dao.CoreIndicatorsRuleDao;
import cn.topevery.garden.entity.po.CoreIndicatorsItem;
import cn.topevery.garden.entity.po.CoreIndicatorsRule;
import cn.topevery.garden.entity.query.CoreIndicatorsRuleQuery;
import cn.topevery.garden.entity.vo.CoreIndicatorsRuleVO;
import cn.topevery.garden.framework.base.BaseDaoImpl;
import hw.topevery.framework.db.CommandType;
import hw.topevery.framework.db.entity.DataPager;
import hw.topevery.framework.db.entity.SqlQueryMate;
import hw.topevery.framework.db.enums.ScriptConditionEnum;
import hw.topevery.framework.entity.DbCsp;
import hw.topevery.framework.entity.KeyValue;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicReference;

/**
 * 
 *
 * @author code-generate
 * @date 2022-01-05
 */
@Repository
public class CoreIndicatorsRuleDaoImpl extends BaseDaoImpl<CoreIndicatorsRule> implements CoreIndicatorsRuleDao {
    @Override
    public <P extends DbCsp> List<CoreIndicatorsRule> getSearch(P para) {
        return null;
    }

    

    @Override
    public DataPager<CoreIndicatorsRuleVO> listPage(CoreIndicatorsRuleQuery query) {
        String table = "t_core_indicators_rule tcir";

        String columns = "tcir.*";

        int pageIndex = query.pageIndex - 1;
        int pageSize = query.pageSize;

        SqlQueryMate sqlQueryMate = new SqlQueryMate(table).setColumns(columns).setOrderBy("tcir.c_id desc");
        sqlQueryMate
                .where(true, "tcir.c_db_status", ScriptConditionEnum.Eq, 0)
        ;

        return getDataPager(sqlQueryMate, pageSize, pageIndex, CoreIndicatorsRuleVO.class);
    }

    @Override
    public CoreIndicatorsRule getEntityByColumn(String key,String value){
        KeyValue<String> map=new KeyValue<>();
        map.key=key;
        map.value=value;
        return getEntityByColumnEqValue(map);
    }

    @Override
    public List<CoreIndicatorsRule> rules(String code) {
        StringBuffer sb = new StringBuffer();
        sb.append(" select*From t_core_indicators_rule where c_dict_code='"+code+"'");

        AtomicReference<List<CoreIndicatorsRule>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CoreIndicatorsRule.class));
        });

        return vo.get();
    }

    @Override
    public List<CoreIndicatorsItem> info(String code) {
        StringBuffer sb = new StringBuffer();
        sb.append("select b.c_id id,b.c_dict_code code,b.c_dict_name name,0.0 value From (\n" +
                "        SELECT\n" +
                "        SUBSTRING_INDEX(SUBSTRING_INDEX(cr.c_remark,'/',help_topic_id+1),'/',-1) AS spName\n" +
                "        FROM\n" +
                "        mysql.help_topic,(select c_remark From t_sys_dict where c_dict_code='"+code+"' limit 0,1) cr\n" +
                "        WHERE\n" +
                "        help_topic_id <= LENGTH(cr.c_remark)-LENGTH(REPLACE(cr.c_remark,'/',''))+1) a inner join t_sys_dict b on a.spName=b.c_dict_code");

        AtomicReference<List<CoreIndicatorsItem>> vo = new AtomicReference<>(new ArrayList<>());
        run(sb.toString(), CommandType.Text, dbCommand -> {
            vo.set(dbCommand.executeToList(CoreIndicatorsItem.class));
        });

        return vo.get();
    }

    @Override
    public List<Map<String, Object>> infoData(String code, String mapId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT DISTINCT base.c_id AS id, base.c_dict_code AS code, base.c_dict_name AS name\n" +
                "\t, CASE\n" +
                "\t\tWHEN base.num = 1 THEN info.c_numerator\n" +
                "\t\tELSE info.c_denominator\n" +
                "\tEND AS `value`\n" +
                "\t, info.c_is_reach AS `isReach`\n" +
                "\t, info.c_real_value AS `realValue`\n" +
                "FROM (\n" +
                "\tSELECT tsd.c_id,spName,c_dict_code,num,c_dict_name\n" +
                "\tFROM (\n" +
                "\t\tSELECT SUBSTRING_INDEX(SUBSTRING_INDEX(cr.c_remark, '/', help_topic_id + 1), '/', -1) AS spName\n" +
                "\t\t\t, @i := @i + 1 AS num\n" +
                "\t\tFROM mysql.help_topic, (\n" +
                "\t\t\t\tSELECT c_remark, c_dict_code\n" +
                "\t\t\t\tFROM t_sys_dict\n" +
                "\t\t\t\tWHERE c_dict_code ='"+code +
                "'\t\t\t\t LIMIT 0, 1\n" +
                "\t\t\t) cr, (\n" +
                "\t\t\t\tSELECT @i := 0\n" +
                "\t\t\t) it\n" +
                "\t\tWHERE help_topic_id <= LENGTH(cr.c_remark) - LENGTH(REPLACE(cr.c_remark, '/', '')) + 1\n" +
                "\t) a, t_sys_dict tsd\n" +
                "\tWHERE a.spName = tsd.c_dict_code\n" +
                ") base\n" +
                "\tLEFT JOIN (\n" +
                "\t\tSELECT c_numerator, c_denominator,c_is_reach,c_real_value\n" +
                "\t\tFROM t_criterion_def tcd\n" +
                "\t\t\tLEFT JOIN t_criterion_evaluation tce ON tcd.c_criterion_id = tce.c_criterion_id\n" +
                "\t\tWHERE tce.c_map_id='"+mapId+"' and  tcd.c_expression ='"+ code+"' \tAND tce.c_year = DATE_FORMAT( NOW(), '%Y' ) \n\n" +
                "\t) info on 1=1");

        return getMap(sb.toString());

    }

    @Override
    public List<Map<String, Object>> infoDataShow(String code, String mapId) {
        StringBuffer sb = new StringBuffer();
        sb.append("SELECT DISTINCT base.c_id AS id, base.c_dict_code AS code, base.c_dict_name AS name\n" +
                "\t, CASE\n" +
                "\t\tWHEN base.num = 1 THEN info.c_numerator\n" +
                "\t\tELSE info.c_denominator\n" +
                "\tEND AS `value`\n" +
                "\t, info.c_is_reach AS `isReach`\n" +
                "\t, info.c_real_value AS `realValue`\n" +
                "FROM (\n" +
                "\tSELECT tsd.c_id,spName,c_dict_code,num,c_dict_name\n" +
                "\tFROM (\n" +
                "\t\tSELECT SUBSTRING_INDEX(SUBSTRING_INDEX(cr.c_remark, '/', help_topic_id + 1), '/', -1) AS spName\n" +
                "\t\t\t, @i := @i + 1 AS num\n" +
                "\t\tFROM mysql.help_topic, (\n" +
                "\t\t\t\tSELECT c_remark, c_dict_code\n" +
                "\t\t\t\tFROM t_sys_dict\n" +
                "\t\t\t\tWHERE c_dict_code = '"+code +
                "'\t\t\t\tLIMIT 0, 1\n" +
                "\t\t\t) cr, (\n" +
                "\t\t\t\tSELECT @i := 0\n" +
                "\t\t\t) it\n" +
                "\t\tWHERE help_topic_id <= LENGTH(cr.c_remark) - LENGTH(REPLACE(cr.c_remark, '/', '')) + 1\n" +
                "\t) a, t_sys_dict tsd\n" +
                "\tWHERE a.spName = tsd.c_dict_code\n" +
                ") base\n" +
                "\tLEFT JOIN (\n" +
                "\t\tSELECT c_numerator, c_denominator,c_is_reach,c_real_value\n" +
                "\t\tFROM t_criterion_def tcd\n" +
                "\t\t\tLEFT JOIN t_criterion_evaluation_show tce ON tcd.c_criterion_id = tce.c_criterion_id\n" +
                "\t\tWHERE tce.c_map_id='"+mapId+"' and  tcd.c_expression ='"+ code+"' \tAND tce.c_year = DATE_FORMAT( NOW(), '%Y' ) \n" +
                "\t) info on 1=1");


        return getMap(sb.toString());
    }

    @Override
    public List<Map<String, Object>> coreList() {
      return  getMap("SELECT\n" +
                "            tsd.c_dict_name `dictName`,\n" +
                "            tsd.c_dict_code `dictCode`,\n" +
                "            (select dict.c_dict_name from t_sys_dict dict where dict.c_db_status = 0 and dict.c_ns_id = 'eefa4ef3-b23b-4b91-aece-fc9bb38cdd95' and dict.c_dict_code = tce.c_operator) as operatorStr,\n" +
                "            tce.c_operator as operator,\n" +
                "            tce.c_value as value,\n" +
                "            tce.c_unit as unit\n" +
                "        FROM\n" +
                "            t_criterion_def tcd\n" +
                "                JOIN t_sys_dict tsd ON tcd.c_expression = tsd.c_dict_code\n" +
                "                left join t_criterion_expand tce on tcd.c_criterion_id = tce.c_criterion_id and tce.c_db_status = 0\n" +
                "        WHERE\n" +
                "            tcd.c_is_core_indicator = 0\n" +
                "          AND tcd.c_db_status = 0\n" +
                "          AND tcd.c_expression IS NOT NULL\n" +
                "          AND tcd.c_expression != ''\n" +
                "        ORDER BY\n" +
                "            tsd.c_order_num");
    }
}
